package cn.edu.nju.oraldefense.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import cn.edu.nju.oraldefense.model.Graduate;
import cn.edu.nju.oraldefense.model.GraduateOperation;
import cn.edu.nju.oraldefense.model.Paper;
import cn.edu.nju.oraldefense.service.CommentService;
import cn.edu.nju.oraldefense.service.GraduateOperationService;
import cn.edu.nju.oraldefense.service.PaperService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PaperAddAction extends ActionSupport {

	private static final int BUFFER_SIZE = 16 * 1024;
	private String title;
	private File upload;
	private String uploadFileName;// struts2 自动填写
	private String savePath;// 依赖注入的路径

	private PaperService paperService;
	private CommentService commentService;
	private GraduateOperationService operationService;

	private boolean success;

	@JSON(serialize = false)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@JSON(serialize = false)
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	@JSON(serialize = false)
	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	@JSON(serialize = false)
	public PaperService getPaperService() {
		return paperService;
	}

	@Resource
	public void setPaperService(PaperService paperService) {
		this.paperService = paperService;
	}

	@JSON(serialize = false)
	public CommentService getCommentService() {
		return commentService;
	}

	@Resource
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	@JSON(serialize = false)
	public GraduateOperationService getOperationService() {
		return operationService;
	}

	@Resource
	public void setOperationService(GraduateOperationService operationService) {
		this.operationService = operationService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	private static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public String execute() throws Exception {

		ActionContext ctx = ActionContext.getContext();
		Graduate graduate = (Graduate) ctx.getSession().get("graduate");
		int dotPos = uploadFileName.lastIndexOf('.');
		String dstFileName = graduate.getNumber() + "_" + title
				+ uploadFileName.substring(dotPos);

		// 根据服务器的文件保存地址和原文件名创建目录文件全路径
		String dstPath = ServletActionContext.getServletContext().getRealPath(
				this.getSavePath())
				+ "\\" + dstFileName;
		File dstFile = new File(dstPath);
		copy(this.upload, dstFile);

		Paper paper = paperService.getByGraduate(graduate);

		if (paper == null) {
			paper = new Paper();
			paper.setFile(dstFileName);
			paper.setGraduate(graduate);
			paper.setState(0);
			paper.setTitle(title);
			paperService.addPaper(paper);
			
			GraduateOperation operation = new GraduateOperation();
			operation.setGraduate(graduate);
			operation.setTime(Calendar.getInstance().getTime());
			operation.setType(GraduateOperation.OPERATION_ADD);
			operationService.addOperation(operation);
		} else {
			paper.setFile(dstFileName);
			paper.setGraduate(graduate);
			paper.setState(0);
			paper.setTitle(title);
			paperService.modifyPaper(paper);
			
			GraduateOperation operation = new GraduateOperation();
			operation.setGraduate(graduate);
			operation.setTime(Calendar.getInstance().getTime());
			operation.setType(GraduateOperation.OPERATION_MODIFY);
			operationService.addOperation(operation);
		}
		success = true;
		return SUCCESS;
	}
}
